<?php
namespace data\sys\images;
use \data\BaseData;
/**
 * 图片管理
 */
class ImageData extends BaseData{
    
    /**
     * 读取图片列表
     */
    public function getlist($param){
        $page = $param['page'] ?: 1;
        $limit= $param['limit']?: 20;
        $check = ['a.albumid','a.addtime','a.status','b.utype','b.uid'];
        $where = $this->parseWhere($check,$param);
        if( !$where )  dataE('查询条件不能为空');
        $field = 'a.*';
        $list  = M('sys_image')->alias('a')
            ->join('elite_sys_image_album b','a.albumid = b.id')
            ->where($where)->page($page)->field($field)->limit($limit)->order('a.id desc')->select();
        $total = M('sys_image')->alias('a')
            ->join('elite_sys_image_album b','a.albumid = b.id')
            ->where($where)->count();
        return [
            'list' => $list,
            'total'=> $total,
            'page' => $page,
            'limit'=> $limit
        ];
    }
    
    /**
     * 添加
     */
    public function add($data){
        $check = [            
            'albumid'   => ['check'=>'must','msg'=>'相册不能为空','reg'=>'int'],
            'picurl'    => ['check'=>'must','msg'=>'图片路径不能为空'],
            'picname'   => '',
            'size'      => '',
            'addtime'   => ['val'=>SYS_TIME],
            'status'    => ['val'=>1]            
        ];
        $save = $this->verifyData($check,$data);
        return M('sys_image')->insertGetId($save);
    }
    
    /**
     * 添加
     */
    public function edit($id,$data){
        if( !$id ) dataE('相册ID不能为空');
        $check = [            
            'albumid'   => ['reg'=>'int','msg'=>'相册不能为空'],
            'picurl'    => ['reg'=>'must','msg'=>'图片路径不能为空'],
            'picname'   => '',
            'status'    => ['reg'=>[1,2]]            
        ];
        
        $where = ['id'=> $id];
        $save = $this->verifyData($check,$data);
        $result = M('sys_image')->where($where)->update($save);
        if( $result === false ) dataE('修改相册信息失败');
    }
}